import 'package:backend.purchases/domain/entities/yookassa_api/payments/yookassa_api_pending_payment.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/pending_payment/yookassa_api_pending_payment_confirmation_info_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/pending_payment/yookassa_api_pending_payment_confirmation_info_dto_factory.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/pending_payment/yookassa_api_pending_payment_payment_method_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/yookassa_api_payment_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/yookassa_api_payment_meta_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/yookassa_api_money_amount_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/yookassa_api_time_dto.dart';

const kYookassaApiPendingPaymentConfirmationInfoKey = 'confirmation';
const kYookassaApiPendingPaymentMethodKey = 'payment_method';
const kYookassaApiPendingPaymentCreationTimeKey = 'created_at';

const kYookassaApiPendingPaymentTypeValue = 'pending';

class YookassaApiPendingPaymentDto extends YookassaApiPaymentDto {
  const YookassaApiPendingPaymentDto._internal({
    required this.id,
    required this.amountDto,
    required this.metadataDto,
    required this.test,
    required this.confirmationInfoDto,
    required this.paymentMethodDto,
    required this.creationTimeDto,
  });

  @override
  final String id;

  @override
  final YookassaApiMoneyAmountDto amountDto;

  @override
  final YookassaApiPaymentMetadataDto metadataDto;

  @override
  final bool test;

  final YookassaApiPendingPaymentConfirmationInfoDto confirmationInfoDto;
  final YookassaApiPendingPaymentMethodDto paymentMethodDto;
  final YookassaApiTimeDto creationTimeDto;

  @override
  String get type => kYookassaApiPendingPaymentTypeValue;

  @override
  List<Object?> get props {
    return [
      id,
      amountDto,
      metadataDto,
      test,
      confirmationInfoDto,
      paymentMethodDto,
      creationTimeDto,
    ];
  }

  factory YookassaApiPendingPaymentDto.fromJson(Map<String, dynamic> json) {
    final metadataJson = json[kYookassaApiPaymentMetadataKey];
    final metaDto = YookassaApiPaymentMetadataDto.fromJson(metadataJson);

    final amountJson = json[kYookassaApiPaymentAmountKey];
    final amountDto = YookassaApiMoneyAmountDto.fromJson(amountJson);

    final confirmationInfoJson = json[kYookassaApiPendingPaymentConfirmationInfoKey];
    final confirmationInfoDto =
        YookassaApiPendingPaymentConfirmationInfoDtoFactory().fromJson(confirmationInfoJson);

    final paymentMethodJson = json[kYookassaApiPendingPaymentMethodKey];
    final paymentMethodDto = YookassaApiPendingPaymentMethodDto.fromJson(paymentMethodJson);

    final creationTimeString = json[kYookassaApiPendingPaymentCreationTimeKey];
    final creationTimeDto = YookassaApiTimeDto.fromString(creationTimeString);

    return YookassaApiPendingPaymentDto._internal(
      id: json[kYookassaApiPaymentIdKey],
      amountDto: amountDto,
      metadataDto: metaDto,
      test: json[kYookassaApiPaymentTestKey],
      confirmationInfoDto: confirmationInfoDto,
      paymentMethodDto: paymentMethodDto,
      creationTimeDto: creationTimeDto,
    );
  }

  @override
  YookassaApiPendingPayment toEntity() {
    final amount = amountDto.toEntity();
    final confirmationInfo = confirmationInfoDto.toEntity();
    final paymentMethod = paymentMethodDto.toEntity();
    final creationTime = creationTimeDto.toEntity();

    return YookassaApiPendingPayment(
      id: id,
      amount: amount,
      purchaseItemSku: metadataDto.purchaseItemSku,
      userId: metadataDto.userId,
      savedPaymentMethodId: metadataDto.savedPaymentMethodId,
      test: test,
      confirmationInfo: confirmationInfo,
      paymentMethod: paymentMethod,
      creationTime: creationTime,
    );
  }
}
